/*
 *
 * SimplePCR1000 (Qt4) Version 1.x
 * Copyright (C) 2011  Tyler H. Jones (me@tylerjones.me)
 * http://www.thebasementserver.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


#ifndef COMMON_H
#define COMMON_H

#include <QtGui/QDialog>
#include <QtCore/QObject>
#include <QtCore/QCoreApplication>
#include <QtGui/QApplication>
#include <QObject>
#include <QThread>
#include <QMainWindow>
#include <QSettings>

#include <QtGui/QMessageBox>

#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVector>
#include <QtCore/QMap>
#include <QtCore/QList>

#include <QtCore/QProcess>
#include <QtCore/QTime>
#include <QtCore/QTimerEvent>
#include <QtCore/QTimer>

#include <QtGui/QSystemTrayIcon>
#include <QtGui/QMenu>
#include <QtGui/QDesktopWidget>

#include <QtCore/QDebug>
#include <QtCore/QSize>
#include <QtCore/QVariant>
#include <QtCore/QModelIndex>
#include <QtCore/QEvent>

#include <QtGui/QAction>
#include <QtGui/QCloseEvent>
#include <QtCore/QTextStream>
#include <QtCore/QIODevice>
#include <QtSql/QtSql>
#include <QAbstractButton>

#ifdef Q_WS_WIN
    #include <QDesktopServices>
    #include <fstream>
    #include <windows.h>
    #include <wininet.h>
    #include <shlobj.h>
    #include <QSharedMemory>
    #include <QSettings>
#endif

#ifdef Q_WS_MAC
    #include <strings.h>
    #include <stdlib.h>
    #include <sys/sysctl.h>
#endif

#include <cstdlib>
#include <stdio.h>
#include <iostream>

/*****************************************************************
*** Constant definitions *****************************************
*****************************************************************/

#define APP_NAME "SimplePCR1000"
#define APP_COMPANY "BitAtlatis"
#define APP_DEVELOPER "Tyler H. Jones"
#define APP_VERSION "1.0.2"
#define APP_STABILITY "Alpha"
#define APP_WEBSITE "http://sourceforge.net/p/simplepcr1000"
#define APP_DEV_EMAIL "me@tylerjones.me"
#define APP_BUGSEMAIL "bugs@autowallpaper.org"

#ifdef Q_WS_X11
    #define APP_DIR ".simplepcr1000"
    #define APP_DBFILE "simplepcr1000.db"
    #define APP_DBFILE_BKP "simplepct1000.db.backup"
    #define APP_RUNDIR "run"
    #define APP_LOCKFILE "simplepcr1000.lock"
    #define APP_DBVERSION "1"
#endif

#ifdef Q_WS_WIN
    #define APP_DIR "simplepcr1000"
    #define APP_DBFILE "simplepcr1000.db"
    #define APP_DBFILE_BKP "simplepct1000.db.backup"
    #define APP_DBVERSION "1"
#endif

#ifdef Q_WS_MAC
    #define APP_DIR ".simplepcr1000"
    #define APP_DBFILE "simplepcr1000.db"
    #define APP_DBFILE_BKP "simplepct1000.db.backup"
    #define APP_DBVERSION "1"
#endif

#ifdef Q_WS_WIN
    #define DEFAULT_Comport "COM1"
#else
    //#define ComPort "/dev/ttyS0" //Real serial port
    #define DEFAULT_ComPort "/dev/ttyUSB0" //USB->Serial adapter
#endif

#define CR "\x0d"
#define LF "\x0a"
#define CL "\x0d\x0a"

#define POWER_ON "H101" //                          Radio Power ON
#define POWER_OFF "H100" //                         Radio Power OFF
#define VOL "J40" //J40xx - xx = 00 to FF           Volume Level
#define SQU "J41" //J41xx - xx = 00 to FF           Squelch Level
#define IFSHIFT "J43" //J43xx - xx = 00 to FF        IF Frequency Shift
#define AGC_ON "J4501" //J45xx - 00 = OFF, 01 = ON       Automatic Gain Control
#define AGC_OFF "J4500" //J45xx - 00 = OFF, 01 = ON       Automatic Gain Control
#define NB_ON "J4601" //J46xx - 00 = OFF, 01 = ON        Noise Blanker
#define NB_OFF "J4600" //J46xx - 00 = OFF, 01 = ON        Noise Blanker
#define ATT_ON "J4701" //J47xx - 00 = OFF, 01 = ON       Attenuator
#define ATT_OFF "J4700" //J47xx - 00 = OFF, 01 = ON       Attenuator
#define VSC_ON "J5001" //J50xx - 00 = OFF, 01 = ON       VSC
#define VSC_OFF "J5000" //J50xx - 00 = OFF, 01 = ON       VSC
#define TSQU "J51" //J51xx - 00 = OFF, J5101 = 67Hz, J5133 = 254.1Hz
#define FREQ_HEAD "K0" //Frequency = FREQ_HEAD G(GHz) MMM(MHz) KKK(KHz) HHH(Hz) mm(Mode) ff(Filter) FREQ_TAIL
#define FREQ_TAIL "00"
#define NO_AUTOUP "G300"
#define EN_AUTOUP "G301"
#define NO_BIN_AUTOUP "G302"
#define EN_BIN_AUTOUP "G303"
#define RATE300 "G100"
#define RATE1200 "G101"
#define RATE4800 "G102"
#define RATE9600 "G103"
#define RATE19200 "G104"
#define RATE38400 "G105"

#define GET_POWER "H1\?"
#define GET_DSP "GD\?"
#define GET_DTMF "I3\?"
#define GET_SQU_STATUS "I0\?"
#define GET_SIG_STREN "I1\?"
#define GET_SIG_FREQ "I2\?"
//Radio replies
#define DSP_YES "GD01"
#define DSP_NO "GD00"
#define GOOD_CMD "G000"
#define BAD_CMD "G001"
#define SQU_STATUS "I0" //I004 = Closed, I007 = Open
#define SIG_STREN "I1" //I1xx - xx = 00 to FF
#define SIG_FREQ "I2" //I200 = Signal Frequency < Display Frequency, I280 = Signal Frequency = Display Frequency, I2FF = Signal Frequency > Display Frequency
#define NO_DTMF "I300"
#define DTMF_TONE "I31" //I31t t is DTMF tone (* = E, # = F)

//Messagebox data
const QString MB_InvalidFreq_T = "Invalid Frequency!";
const QString MB_InvalidFreq_B = "The frequency value you provided is invalid. Format = G(GHz)MMM(MHz).KKK(KHz)HHH(Hz) Example = 1101.125000";

const QString MB_ConfirmNotRunning_T = APP_NAME" Already Running?";
const QString MB_ConfirmNotRunning_B = "Are you sure that "APP_NAME" is not already running?";

const QString MB_AlreadyRunning_T = APP_WEBSITE" Already Running!";
const QString MB_AlreadyRunning_B = APP_WEBSITE" is already running! Press Ok to quit this instance.";

//Confirm Exit
const QString MB_ConfExit_T = "Confirm Quit";
const QString MB_ConfExit_B = "Are you sure you want to quit "APP_NAME"?";

//Information Window
const QString MB_GettingStarted_T = "Getting Started With SimplePCR1000";
const QString MB_GettingStarted_B = QString("<center><b><big>Getting Started With "APP_NAME"</bing></b></center><br><br>") +
                                         "First, be sure to select the serial port that the PCR radio is connected to.<br>" +
                                         "If you are using a USB<->Serial adapter, be sure you have all the required drivers installed.<br>";

//System Tray first run message baloon
const QString ST_FirstRunMsg_T = "This is where I live.";
const QString ST_FirstRunMsg_B = QString("When "APP_NAME" is running, this icon will be displayed here in your system tray. ") +
                                         "You can close and hide the settings dialog but "APP_NAME" will continue to run in the background. " +
                                         "Double click on the icon to reveal the dialog or right click for more options.";

//Confirm Restoring Defaut values for all settings
const QString MB_ConfirmRestoreDefaults_T = "Confirm Major Action";
const QString MB_ConfirmRestoreDefaults_B = "Are you sure you want to restore "APP_NAME"'s settings to their default values?";

//Confirm reset of DB
const QString MB_ConfirmResetDB_T = "Confirm Major DB Action";
const QString MB_ConfirmResetDB_B = "Are you sure you want to reset the scanner channels database?\nNOTE: All channels will be permanently lost!";

//Confirm reset of DB
const QString MB_ConfirmRestoreDB_T = "Confirm Major DB Action";
const QString MB_ConfirmRestoreDB_B = "Are you sure you want to restore the scanner channels database from the backup database file?\nNOTE: All existing channels will be permanently overwritten the scanner channels database!";

//DB error messages
const QString MB_DBRestoreFailed_T = "DB Restore Failed!";
const QString MB_DBRestoreFailed_B = APP_NAME" failed to restore the database from the backup database file!\nCheck the existence of the files and that you have permission to R/W "APP_NAME"'s settings directory '$HOME/simplepcr1000/'";

const QString MB_DBBackupFailed_T = "DB Backup Failed!";
const QString MB_DBBackupFailed_B = APP_NAME" failed to backup the scanner channels database!\nCheck the existence of the files and that you have permission to R/W "APP_NAME"'s settings directory '$HOME/simplepcr1000/'";

const QString MB_DBBackupMissing_T = "DB Backup Missing!";
const QString MB_DBBackupMissing_B = APP_NAME" cannot find the database backup file! Be sure the backup file is where it belongs:\n'$HOME/simplepcr1000/simplepcr1000.db.backup'";

const QString MB_DBBackupSuccess_T = "DB Backup Finished!";
const QString MB_DBBackupSuccess_B = "The scanner channels database was successfully backed-up!";

//Settings
#define S_FIRSTRUN "IsFirstRun"
#define S_F15RUNS "IsFirst15Runs"
#define S_AUTOSTART "AutoStartup"
#define S_SCAN "IsScannerActive"
#define S_PORT "SerialPort"
#define S_FREQ "CurFreq"
#define S_VOL "CurVolume"
#define S_MUTE "CurMute"
#define S_SQU "CurSquelch"
#define S_MODE "CurMode"
#define S_FILTER "CurFilter"
#define S_IFSHIFT "CurIFShift"
#define S_AGC "CurAutomaticGainControl"
#define S_VSC "CurVSC"
#define S_NB "CurNoiseBlanking"
#define S_ATTEN "CurAttenuation"
#define S_SCAN_IVAL "ScannerInterval"
#define S_DELAY_IVAL "DelayInterval"
#define S_DATARATE "DataRate"
#define S_STARTHIDDEN "HideOnStartup"
#define S_STARTON "StartPowerOn"
#define S_CHANNOTIF "ChannelNotifs"

/*****************************************************************
*** END Constant definitions *************************************
*****************************************************************/

class Common : public QObject
{
    Q_OBJECT

    public:
    Common();
    ~Common();
    QString qvar(QVariant var);
    int b2n(bool b);
    bool n2b(int n);
    QString PadFreq(QString freq);

    QStringList ComPortValues;
    QStringList ModeComboBox;
    QStringList FilterComboBox;
    QStringList DataRates;

    QMap<QString, QString> ModeValues;
    QMap<QString, QString> FilterValues;
    QMap<QString, double> StepSizeValues;

};

#endif // COMMON_H
